package org.wmine.preprocess;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
public class Dimensionality extends JFrame implements ActionListener
{
	JLabel lblSelect;
	JCheckBox []column;
	JButton btnOK;
	DefaultTableModel dtm;
	public Dimensionality(DefaultTableModel dtm)
	{
		this.setLayout(null);
		this.setBounds(250,100,300,400);
		this.setTitle("Dimensionality Reduction");
		this.dtm=dtm;
		
		lblSelect=new JLabel("Select Attributes you want to REMOVE");
		lblSelect.setBounds(40,50,250,25);
		int colcnt=this.dtm.getColumnCount();
		column=new JCheckBox[colcnt];
		int y=0;
		for(int ii=0;ii<colcnt;ii++)
		{
			column[ii]=new JCheckBox(this.dtm.getColumnName(ii));
			//System.out.print("ii==>"+columnNames[ii]);
			column[ii].setBounds(40,y+85,200,25);
			y+=25;
			//colName[ii].addItemListener(this);
		    //colName[ii].setSelected(true);
		    this.add(column[ii]);
		    
		}

		btnOK=new JButton("OK");
		btnOK.setBounds(100,y+85,100,25);
		btnOK.addActionListener(this);
		
		this.add(lblSelect);
		this.add(btnOK);
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		int colcnt=dtm.getColumnCount();
		ArrayList l1=new ArrayList();
		Vector col=new Vector();
		PreprocessPanel.report.append("<br>You chose to perform Dimensionality Reduction<br>");
		PreprocessPanel.report.append("<br>Attribute/Column deleted: ");
		PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
		
		if(ae.getSource()==btnOK)
		{	int cnt=1;
			for(int ii=0;ii<colcnt;ii++)
	    	{
				col.add(dtm.getColumnName(ii));
	    		   if(column[ii].isSelected()==true)
	    		   {
	    			    			   	    			  
	    			   PreprocessPanel.report.append("<br>"+cnt+" : "+this.dtm.getColumnName(ii));
	    			   PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
	    				
	    			   //System.out.println("yes");
	    			   l1.add(ii);
	    			   cnt++;
	    			   System.out.println("this is the list===>"+col);
	    			   
	    		   }
	    		   
	    	}
			PreprocessPanel.summary.repaint();
			removeAttribute(l1,col);
			this.dispose();
		}
	}
	private void removeAttribute(ArrayList l1,Vector col)
	{
		Vector v=this.dtm.getDataVector();
		int cnt=0;
		Iterator e1=l1.iterator();
		while(e1.hasNext())
		{
			
			int i=Integer.parseInt(""+e1.next());
			Iterator e=v.iterator();
			
			while(e.hasNext())
			{
				Vector o=(Vector) e.next();
				o.removeElementAt(i-cnt);
				//System.out.println("o "+o);
			}
			
			col.removeElementAt(i-cnt);
			
			cnt++;
		}
		this.dtm.setDataVector(v,col);
	}
}